import { Message } from 'element-ui'
import axios from '../../js/axios'
import qs from 'qs' // 序列化时候用到
export default {
    namespaced: true,
    state: {},
    getters: {},
    mutations: {},
    actions: {
    // get方式
        async get ({ commit }, params) {
            try {
                let { data: { success, data, errorMessage } } = await axios.get('url', { params: params })
                return new Promise((resolve, reject) => {
                    if (success) {
                        resolve(data)
                    } else {
                        Message.error(errorMessage)
                        reject(data)
                    }
                })
            } catch {
                Message.error('服务器出错')
            }
        },
        // post方式 + 序列化格式
        async post ({ commit }, params) {
            try {
                let { data: { success, data, errorMessage } } = await axios.post('login', qs.stringify(params))
                return new Promise((resolve, reject) => {
                    if (success) {
                        resolve(data)
                    } else {
                        Message.error(errorMessage)
                        reject(data)
                    }
                })
            } catch {
                Message.error('服务器出错')
            }
        },
        // post方式
        async getInitData ({ commit }, params) {
            try {
                let { data: { success, data, errorMessage } } = await axios.post('login', params)
                return new Promise((resolve, reject) => {
                    if (success) {
                        resolve(data)
                    } else {
                        Message.error(errorMessage)
                        reject(data)
                    }
                })
            } catch {
                Message.error('服务器出错')
            }
        }
    }
}
